<template>
	<view style="height: 100vh;">
			
		<top-heigth :backTitle="backTitle"></top-heigth>
		<view class="content-box-bootom" v-if="sign == 1" @click="start">
			<image class="contem-images" src="/static/image/kaishiluyin.png" alt=""></image>
		</view>
		<view class="content-box-bootom" v-else @click="endluyin">
			<image class="contem-images" src="/static/image/tingzhiluyin.png" alt=""></image>
		</view>
	</view>
</template>

<script>
	import TopHeigth from '@/components/TopHeigth.vue'
	const recorderManager = uni.getRecorderManager();//创建一个录音实例
	const innerAudioContext = uni.createInnerAudioContext();//用来播放的实例
	
	
	export default {
		data() {
			return {
				backTitle:"录音",
				userInfo: uni.getStorageSync('userInfo'),
				sign: 1,
				voicePath: ''
			}
		},
		components: { TopHeigth },
		onLoad() {
			let self = this;
			recorderManager.onStop(function (res) {
				console.log('recorder stop' + JSON.stringify(res));
				self.voicePath = res.tempFilePath;
				self.uploadFilePromise(res.tempFilePath).then(res => {
					console.log(res)
					// 后台进行存储
					uni.$u.http.post('/api/shop/v1_0/tAppVoiceRCA/insertTAppVoice',{
						staffId:self.userInfo.id,
						url: res
					}).then((res) => {
						console.log(res)
					})
				})
			});
		},
		methods: {
			start(){
				this.sign = 2
				
				this.timecount = '00:00:00';//初始化录音时间
				this.hour = 0;
				this.minute = 0;
				this.second = 0;
				const options = {//参数
				    duration: 600000,
				    sampleRate: 44100,
				    numberOfChannels: 1,
				    encodeBitRate: 192000,
				    format: 'mp3',
				    frameSize: 50
				}
				// 获取app的权限状态数据
				let MainActivity = plus.android.runtimeMainActivity();
				let PermissionCheck = MainActivity.checkSelfPermission('android.permission.RECORD_AUDIO')
				if(PermissionCheck != 0){
					uni.showModal({
							title: '提示',
							content: '为了实现录音功能，需要访问您的录音权限，您如果拒绝将无法使用该功能',
							// showCancel: false, // 不显示取消按钮
							success(res) {
								if (res.confirm) {
									recorderManager.start(options);
								}
							}
						});
					}else{
						recorderManager.start(options);
					}
				
			},
			endluyin(){
				this.sign = 1
				
				recorderManager.stop();//结束录音
				
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: uni.$u.http.getSignUrl("/api/system/loginController/uploadImage"), // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						success: (res) => {
							resolve(JSON.parse(res.data).data)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@mixin flex($direction: row) {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: $direction;
	}
	.uv-nav-slot {
		@include flex;
		align-items: center;
		justify-content: space-between;
		border-width: 0.5px;
		border-radius: 100px;
		border-color: #dadbde;
		padding: 3px 7px;
		opacity: 0.8;
	}
	.content-box-bootom{
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		height: 150rpx;
		background-color: #fff;
	}
	.contem-images{
		position: absolute;
		left: 50%;
		top: -50%;
		width: 200rpx;
		height: 200rpx;
		margin-left: -100rpx;
	}
</style>
